package com.huabo.fxgl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huabo.fxgl.entity.Organization;
import com.huabo.fxgl.mapper.OrganizationMapper;
import com.huabo.fxgl.service.IOrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kangjx
 * @since 2022-07-13
 */
@Service
public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Organization> implements IOrganizationService {

    @Autowired
    OrganizationMapper organizationMapper;

    @Override
    public List<Organization> findOrganizationByOrgid(String orgid) {

        return organizationMapper.findOrganizationByOrgid(orgid);

    }

    @Override
    public List<Organization> findOrganizationAll() {
        return organizationMapper.findOrganizationAll();
    }

    @Override
    public void initOrgByOrgtype(int orgtype, String orgname, String orgid) {
        QueryWrapper<Organization> query = new QueryWrapper<>();
        query.eq("orgtype",orgtype);
        query.eq("FATHERORGID",-1);
        query.eq("icode",orgid);
        String hyzskType = null;
        if(orgtype == Organization.TYPE_HY_ZSK){
            hyzskType = orgname;
            query.eq("HYZSKTYPE",orgname);
        }
        long num = this.baseMapper.selectCount(query);
        if(num ==0){
            Organization o = new Organization();
            o.setOrgname(orgname);
            o.setFatherorgid(new BigDecimal("-1"));
            o.setOrgnumber(orgtype + "");
            o.setOrgtype(new BigDecimal(orgtype));
            o.setIcode(orgid);
            o.setHyzsktype(hyzskType);
            organizationMapper.insert(o);
        }
    }

    @Override
    public String findOrgTreeByOrgtypeAndOrgid(String orgid, int orgtype, String hyzskType) {
        String tyStr = "";
        List<Organization> list = organizationMapper.findOrgTreeByOrgtypeAndOrgid(orgid,orgtype,hyzskType);
        String str = "";
        for (int i = 0; i < list.size(); i++) {
            Organization o = list.get(i);
            str += "tree.nodes['" + (i == 0 ? -1 : o.getFatherorgid()) + "_" + o.getOrgid() + "']=\"text:" + o.getOrgname() + ";method:check(" + o.getOrgid() + "," + o.getOrgtype() + ")\";\n";
        }
        return str;
    }


    @Override
    public String getOrgTree(String pageurl) {
        String tree = "";
        try {
            List<Organization> list = organizationMapper.query_orgtype();
            for(Organization org: list){
                if(org == null || org.getOrgid()==null || org.getFatherorgid() == null || org.getOrgname()==null){continue;}
                tree += "tree.nodes[\'" +
                        org.getFatherorgid().toEngineeringString() + "_" + org.getOrgid().toString() +
                        "\']=\"text:" + org.getOrgname() + ";url:" + pageurl +"?pid=" + org.getOrgid().toString() +
                        "\";\n";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tree;
    }


}
